package com.nkzy.Controller;
import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.DeptFormDTO;
import com.nkzy.domain.pojo.Dept;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.vo.DeptVO;
import com.nkzy.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping
    @Operation(summary = "保存用户信息")
    public Result saveUser(@RequestBody DeptFormDTO deptFormDTO){

        Dept dept = BeanUtil.copyProperties(deptFormDTO, Dept.class);

        deptService.save(dept);

        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询一个用户")
    public Result getDeptById(@PathVariable Integer id) {

        Dept dept = deptService.getById(id);

        DeptVO deptVO = BeanUtil.copyProperties(dept, DeptVO.class);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    public Result deleteDeptById(@PathVariable Integer id) {

        deptService.removeById(id);
        return Result.success();
    }

    @GetMapping
    @Operation(summary = "根据id查询多个用户")
    public Result getDepts(@RequestParam("ids") List<Integer> ids){

        List<Dept> depts=deptService.listByIds(ids);

        List<DeptVO> deptVO=BeanUtil.copyToList(depts,DeptVO.class);
        return Result.success();
    }

    @GetMapping("/all")
    @Operation(summary = "查询所有")
    public Result getAllUsers(){

        List<Dept> dept=deptService.list();

        List<DeptVO> deptVO=BeanUtil.copyToList(dept,DeptVO.class);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改用户")
    public Result updateUser(@RequestBody DeptFormDTO deptFormDTO){

        Dept dept = BeanUtil.copyProperties(deptFormDTO, Dept.class);

        deptService.updateById(dept);

        return Result.success();
    }
}
